/**
 * @Description:support.js
 * @Author:LCore
 */


var documentWidth = window.screen.availWidth;
var gridContainerWidth = 0.92*documentWidth;
var cellSideLength = 0.18*documentWidth;
var cellSpace = 0.04*documentWidth;
var headerWidth=window.screen.availWidth;
var showText = {
    2    : '高雄萌妹',
    4    : '一发入魂',
    8    : '乱斗王者',
    16   : 'CAD大师',
    32   : '斗奶狂魔',
    64   : '认发作妈',
    128  : '下岗女工',
    256  : '梦碎豪门',
    512  : '太极宗师',
    1024 : '远离魔都',
    2048 : '文学巨匠',
    4096 : '分手大师',
    8192 : '金牌僚机',
    16384: '股海明灯',
};

var showBackgroundColor = {
    2    : "#eee4da",
    4    : "#ede0c8",
    8    : "#f2b179",
    16   : "#f59563",
    32   : "#f67c5f",
    64   : "#f65e3b",
    128  : "#edcf72",
    256  : "#edcc61",
    512  : "#9c0",
    1024 : "#33b5e5",
    2048 : "#09c",
    4096 : "#a6c",
    8192 : "#93c",
};

function getPostionTop(i, j) {
    return (i * cellSideLength + (i + 1) * cellSpace);
}

function getPostionLeft(i, j) {
    return (j * cellSideLength + (j + 1) * cellSpace);
}

function getShowTextByNum(number){
    if (number < (16384 +1)) {
        return showText[number];
    }

    return 'xx';
}

function getBackgroundColorByNum(number) {
    if (number < 16384) {
        return showBackgroundColor[number];
    } else {
        return "black";
    }
}

function getPreColorByNum(number) {
    if (number <= 4)
        return "#776e65";
    return "white";
}

function isNoSpace(board) {
    for (var i = 0; i < 4; i++)
        for (var j = 0; j < 4; j++)
            if (board[i][j] == 0)
                return false;

    return true;
}

function canMoveAll(board){
    res = false;
    for (i=1;i<5;i++) {
        res = canMove(board, i);
        if (res == true) {
            break;
        }
    }
    return res;
}


/**
 *@Desciption:重新构造canMove方法(包括上、下、左、右)
 *@author:LCore
 *@pragram board:棋盘格
 *@pragram direction:移动方向
 */

function canMove(board, direction) {
    if (direction == 1) {
        //左移
        for (var i = 0; i < 4; i++) {
            for (var j = 1; j < 4; j++) {
                if (board[i][j] != 0) {
                    if (board[i][j - 1] == 0 || board[i][j] == board[i][j - 1])
                        return true;
                }

            }
        }
    } else if (direction == 2) {
        //上移
        for (var i = 0; i < 4; i++) {
            for (var j = 1; j < 4; j++) {
                if (board[j][i] != 0) { //可以移动
                    if (board[j - 1][i] == 0 || board[j][i] == board[j - 1][i])
                        return true;
                }
            }
        }
    } else if (direction == 3) {
        //下移
        for (var i = 3; i >= 0; i--) {
            for (var j = 2; j >= 0; j--) {
                if (board[j][i] != 0) { //可以移动
                    if (board[j + 1][i] == 0 || board[j][i] == board[j + 1][i])
                        return true;
                }
            }
        }
    } else if (direction == 4) {
        //右移
        for (var i = 3; i >= 0; i--) {
            for (var j = 2; j >= 0; j--) {
                if (board[i][j] != 0) {
                    if (board[i][j + 1] == 0 || board[i][j] == board[i][j + 1])
                        return true;
                }

            }
        }
    } else
        return false;

}

/**
 *@Description:横向判断是否存在障碍物
 *@pagram row:第几行
 *@pagram col1:开始索引
 *@pagram col2:结束索引
 *@pagram board:棋盘格
 */
function noBlockHorizontal(row, col1, col2, board) {
    for (var i = col1 + 1; i < col2; i++) {
        if (board[row][i] != 0)
            return false;
    }
    return true;
}

/**
 *@Description:纵向判断是否存在障碍物
 *@pagram col:第几列
 *@pagram row1:开始索引
 *@pagram row2:结束索引
 *@pagram board:棋盘格
 */
function noBlockVectal(col, row1, row2, board) {
    for (var i = row1 + 1; i < row2; i++) {
        if (board[i][col] != 0)
            return false;
    }
    return true;
}